#coding=utf-8


from pyquery import PyQuery
from PIL import Image
import cfscrape
import StringIO
import requests
import dama3
import dama


host="https://hashkiller.co.uk"
addr="/md5-decrypter.aspx"
url=host+addr
proxy = {"http":"http://127.0.0.1:7777","https":"https://127.0.0.1:7777"}
request_headers = {"Accept-Language": "en-US,en;q=0.5","User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Referer": url,"Connection": "keep-alive" }

#_COUNT=1

def crack(lines):
    #global _COUNT
    #print lines
    scraper=cfscrape.create_scraper()
    response=scraper.get(host+addr,proxies=proxy)
    #print response.content

    # Save headers and cookies, to be used in next request
    session = requests.session()
    session.headers = response.headers
    session.cookies = response.cookies

    query=PyQuery(response.content)
    capt_path=host+query("#content1_imgCaptcha").attr("src")
    print capt_path

    capt_content=scraper.get(capt_path, proxies=proxy).content
    capt_img=Image.open(StringIO.StringIO(capt_content))
    #capt_img.show()
    capt_img.save('my.png')

    cod=dama.main()
    #cod = dama3.run('my.png')
    print cod
    
    if cod.__len__()!=6:
        dama.reportError()
        return False

    scraper=cfscrape.create_scraper(sess=scraper)
    response=scraper.post(url,data={'ctl00$ScriptMan1': 'ctl00$content1$updDecrypt|ctl00$content1$btnSubmit',
        '__EVENTTARGET': '',
        '__EVENTARGUMENT': '',
        '__VIEWSTATE': query("#__VIEWSTATE").attr("value"),
        '__EVENTVALIDATION': query("#__EVENTVALIDATION").attr("value"),
        query("#content1_lblStatus").parent().find("input").attr("name"): query("#content1_lblStatus").parent().find("input").attr("value"),
        'ctl00$content1$txtInput': lines,
        'ctl00$content1$txtCaptcha': cod,
        '__ASYNCPOST': 'true',
        'ctl00$content1$btnSubmit': 'Submit'},proxies=proxy)
    #print response.content

    inputs=[]
    outputs=[]
    response=PyQuery(response.content)
    outs=response.find("#content1_lblResults").text()
    status=response.find("#content1_lblStatus").text()
    if status.find('The CAPTCHA code you specifed is wrong. Please try again.')!=-1:
        return False
    outputs=outs.split(' ')

    file=open('crack.txt','a')

    for i in range(outputs.__len__()):
        if(outputs[i]=='MD5'):
            file.write(outputs[i-1]+" "+outputs[i+2]+"\n")
            print outputs[i-1]+" "+outputs[i+2]+"\n"
    file.close()
    #_COUNT+=1

    return True


def md5(filename):
    file=open(filename)
    text=file.readlines()


    while text:
        flag=False
        lines=text[:64]
        text=text[64:]
        while flag==False:
            try:
                flag=crack(lines)
            except BaseException:
                print "----Error,Repeat----"
                pass
        print "----complete----"

    return True


if __name__=='__main__':
    md5("md5.txt")